package cn.tedu.management.portal.controller;


import cn.tedu.management.portal.dto.SalaryDTO;
import cn.tedu.management.portal.dto.SalaryNeedDTO;
import cn.tedu.management.portal.ex.InvalidParameterException;
import cn.tedu.management.portal.ex.NotFoundException;
import cn.tedu.management.portal.ex.ServiceException;
import cn.tedu.management.portal.mapper.AttendanceMapper;
import cn.tedu.management.portal.mapper.PerformanceMapper;
import cn.tedu.management.portal.mapper.SalaryMapper;
import cn.tedu.management.portal.model.Salary;
import cn.tedu.management.portal.service.ISalaryService;
import cn.tedu.management.portal.util.R;
import cn.tedu.management.portal.vo.SalaryNeedVO;
import cn.tedu.management.portal.vo.SalaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/portal/salary")
public class SalaryController {

    @Autowired
    ISalaryService salaryService;

    @Autowired
    SalaryMapper salaryMapper;



//  http://localhost:8080//portal/salary/insert?staffId=4&salary=6000.0&cardNo=”12345678910“&level=2.0&state="在职"&allPerformance=30.1&absenceCount=1
    @RequestMapping("/insert")
    public R<Void> addSalaryController(@Valid Salary salary, BindingResult bindingResult, Double allPerformance, int absenceCount,int holiday){

        if(bindingResult.hasErrors()){
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new InvalidParameterException(errorMessage);
        }

        try{
            //将salary、绩效、考勤内容封装到了salarydto里面
            SalaryDTO salaryDTO = new SalaryDTO();
            salaryDTO.setSalary(salary);
            salaryDTO.setAllPerformance(allPerformance);
            salaryDTO.setAbsenceCount(absenceCount);
            salaryDTO.setHoliday(holiday);

            salaryService.addSalary(salaryDTO);
            System.out.println("成功！>>>>>>>>>>>>>>>>>>>>");
            return R.ok();

        }catch (ServiceException e){
            return R.failure(R.State.ERR_EXITE_MESSAGE,e);
        }



    }


    //  http://localhost:8080//portal/salary/delete?id=1
    @GetMapping("/delete")
    public R<Void> delectSalary(int id){

        try {
            salaryService.deleteSalaryById(id);
            return R.ok();
        }catch (ServiceException e){
            return R.failure(R.State.ERR_NOT_FOUND,e);
        }


    }
    //  http://localhost:8080/portal/salary/find?id=8&awardValue=100&punishmentValue=20
    @GetMapping("/find")
    public R<SalaryNeedVO> findSalary(SalaryNeedDTO salaryNeedDTO){
        System.out.println(1111111111);

        SalaryVO salaryVO = salaryService.findSalary(salaryNeedDTO.getId());
        System.out.println(">>>>>>>>"+salaryVO);
        SalaryNeedVO salaryNeedVO = new SalaryNeedVO();
        salaryNeedVO.setAllPerformance(salaryVO.getAllPerformance());
        salaryNeedVO.setAbsenceCount(salaryVO.getAbsenceCount());

        Double salaryAll =salaryVO.getSalary()+salaryVO.getAllPerformance()*salaryNeedDTO.getAwardValue()
                +salaryVO.getAbsenceCount()*salaryNeedDTO.getPunishmentValue();
        salaryNeedVO.setSalaryAll(salaryAll);

        salaryNeedVO.setLevel(salaryVO.getLevel());
        salaryNeedVO.setState(salaryVO.getState());

        return R.ok(salaryNeedVO);

    }

   // http://localhost:8080/portal/salary/findAll
    @GetMapping("/findAll")
    public R<List<Salary>> findAll(){
        List<Salary> lists = salaryMapper.findAllSalary();
        System.out.println(lists);
        return R.ok(lists);
    }





}
